package com.ruoyi.question.service.impl;

import java.util.List;

import com.ruoyi.answerRecord.mapper.UserQuizAnswerRecordMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.question.vo.QuestionVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.question.mapper.QuestionInfoMapper;
import com.ruoyi.question.domain.QuestionInfo;
import com.ruoyi.question.service.IQuestionInfoService;

/**
 * 题目管理Service业务层处理
 * 
 * @author amy
 * @date 2025-06-10
 */
@Service
public class QuestionInfoServiceImpl implements IQuestionInfoService 
{
    @Autowired
    private QuestionInfoMapper questionInfoMapper;

    @Autowired
    private UserQuizAnswerRecordMapper userQuizAnswerRecordMapper;

    /**
     * 查询题目管理
     * 
     * @param id 题目管理主键
     * @return 题目管理
     */
    @Override
    public QuestionInfo selectQuestionInfoById(Long id)
    {
        return questionInfoMapper.selectQuestionInfoById(id);
    }

    /**
     * 查询题目管理列表
     * 
     * @param questionInfo 题目管理
     * @return 题目管理
     */
    @Override
    public List<QuestionInfo> selectQuestionInfoList(QuestionInfo questionInfo)
    {
        return questionInfoMapper.selectQuestionInfoList(questionInfo);
    }

    /**
     * 新增题目管理
     * 
     * @param questionInfo 题目管理
     * @return 结果
     */
    @Override
    public int insertQuestionInfo(QuestionInfo questionInfo)
    {
        return questionInfoMapper.insertQuestionInfo(questionInfo);
    }

    /**
     * 修改题目管理
     * 
     * @param questionInfo 题目管理
     * @return 结果
     */
    @Override
    public int updateQuestionInfo(QuestionInfo questionInfo)
    {
        return questionInfoMapper.updateQuestionInfo(questionInfo);
    }

    /**
     * 批量删除题目管理
     * 
     * @param ids 需要删除的题目管理主键
     * @return 结果
     */
    @Override
    public int deleteQuestionInfoByIds(Long[] ids)
    {
        return questionInfoMapper.deleteQuestionInfoByIds(ids);
    }

    /**
     * 删除题目管理信息
     * 
     * @param id 题目管理主键
     * @return 结果
     */
    @Override
    public int deleteQuestionInfoById(Long id)
    {
        return questionInfoMapper.deleteQuestionInfoById(id);
    }

    /**
     * 查询用户题目列表
     *
     * @param questionVO 用户题目
     * @return 用户题目
     */
    @Override
    public List<QuestionVO> selectUserQuestionList(QuestionVO questionVO) {
        Long userId = SecurityUtils.getUserId();

        // 查询题目列表
        List<QuestionVO> questionList = questionInfoMapper.selectUserQuestionList(questionVO);

        // 遍历题目列表，判断用户是否作答过
        for (QuestionVO question : questionList) {
            boolean hasAnswered = userQuizAnswerRecordMapper.existsByUserIdAndQuestionId(userId, question.getId());
            question.setHasAnswer(hasAnswered);
        }

        return questionList;
    }
}
